/*
 * GeoVISTA Center (Penn State, Dept. of Geography)
 * Copyright (c), 2003-2011,  Jin Chen and Alan M. MacEachren, GeoVISTA Center, Penn State University
 * Licensed under Simplified BSD License
 *
 *
 * @Original Author: jin Chen
 * @date: Dec 13, 2004$
 * @version: 1.0
 */
package edu.psu.geovista.app.visualclassifier.classify.v2;

import edu.psu.geovista.jdm.algorithms.BoundaryClassificationSettings;
import edu.psu.geovista.jdm.algorithms.KMeansSettings;
import edu.psu.geovista.jdm.base.JDMUnsupervisedAlgorithmSettings;
import edu.psu.geovista.jdm.base.JDMUnsupervisedSettings;
import edu.psu.geovista.jdm.base.JDMUnsupervisedTask;

public class UnsupervisedSettingTaskFactory {
     /**
      * Make classification based on single variable
      * @param attrName             name of attribute based on which the classification is made
      * @param attrData             data of the attribute
      * @param numOfCateogry        number of classification u want to make
      * @param classifyMethodName   the name of the classify method, it indicate the algorithm for classiication
      * @return
      */
     public static JDMUnsupervisedTask getUniTask(Attribute attr1,  String classifyMethodName,int numOfCateogry) {
                 String[] attrNames=new String[1];  //must defined as array,otherwise JDM will throw classCastException
                Object attrData;
                attrNames[0]=attr1.getName();
                attrData=attr1.getData();
                //create algorithm (classify method)
                JDMUnsupervisedAlgorithmSettings uas[]= new JDMUnsupervisedAlgorithmSettings[2];
                uas[0]=new  BoundaryClassificationSettings(classifyMethodName,numOfCateogry);  //algorithm

                //data
                Object[] objs=new Object[2]; // 
                objs[0] = attrNames;
                objs[1] = attrData;
                int numOfDimension=1; //uni variable

                //combine settings
                JDMUnsupervisedSettings settings=new JDMUnsupervisedSettings(numOfDimension,uas,objs);

                //create task
                JDMUnsupervisedTask task = new JDMUnsupervisedTask(settings);
                return task;
     }
      /**
       * Make classification based on 2 variables
       * @param attrName1                   name of attribute based on which the classification is made
       * @param attrData1                   data of the attribute
       * @param classifyMethodName1         the name of the classify method, it indicate the algorithm for classiication
       * @param numOfCateogry1              number of classification u want to make
       * @param attrName2
       * @param attrData2
       * @param classifyMethodName2
       * @param numOfCateogry2
       * @return
       */
      public static JDMUnsupervisedTask getBiTask(Attribute attr1, String classifyMethodName1,int numOfCateogry1,
                                           Attribute attr2, String classifyMethodName2,int numOfCateogry2) {

                String attrName1;
                Object attrData1;
                String attrName2;
                Object attrData2;

                attrName1=attr1.getName();
                attrData1=attr1.getData();
                attrName2=attr2.getName();
                attrData2=attr2.getData();

                 //create algorithm (classify method)
                JDMUnsupervisedAlgorithmSettings[] uas = new JDMUnsupervisedAlgorithmSettings[2];
                uas[0] = new BoundaryClassificationSettings(classifyMethodName1, numOfCateogry1);
                uas[1] = new BoundaryClassificationSettings(classifyMethodName2, numOfCateogry2);

                //data
                Object[]  objs = new Object[3];
                String[] selectedAttNames = new String[2];
                selectedAttNames[0] = attrName1;
                selectedAttNames[1] = attrName2;
                objs[0] = selectedAttNames;
                objs[1] = attrData1;
                objs[2] = attrData2;
                int numOfDimension=2; //bi variables
                //combine settings
                JDMUnsupervisedSettings settings = new JDMUnsupervisedSettings(numOfDimension, uas, objs);
                JDMUnsupervisedTask task = new JDMUnsupervisedTask(settings);
                return task;
     }

     public static JDMUnsupervisedTask getMultiTask(Attribute[] attrs,String classifyMethodName,int numOfCateogry){
         //create algorithm (classify method)
           JDMUnsupervisedAlgorithmSettings uas[]= new JDMUnsupervisedAlgorithmSettings[2];
           if (classifyMethodName.equals("KMeans") )
                    uas[0] = new KMeansSettings(classifyMethodName, numOfCateogry, null);

           Object[] objs = getObjects(attrs);
           int numOfDimension=3; //uni variable
           JDMUnsupervisedSettings  settings = new JDMUnsupervisedSettings(numOfDimension, uas, objs);
           JDMUnsupervisedTask task = new JDMUnsupervisedTask(settings);
           return task;
     }

     public static Object[] getObjects(Attribute[] attrs){
         if(attrs!=null&&attrs.length >0){
             Object[] datas=new Object[attrs.length+1];
             String[] attrNames=new String[attrs.length ];
             for (int i=0;i<attrs.length ;i++){
                 String attn=attrs[i].getName() ;
                 Object data=attrs[i].getData();

                 datas[i+1]=data;
                 attrNames[i]=attn;
             }
             datas[0]=attrNames;
             return datas;
         }
         else
             return null;

     }
}
